/**************************************************
Class Name:Database
Author's Name:John Lawniczak
Date:04-21-2010
Description of the class: collection of entry's 
	that is populated by a file first and then 
	can be acted upon
***************************************/
import java.util.Scanner;
import java.io.File;

public class Database {
	private String fileAddr;
	private int numberOfEntries;
	private Entry[] list = new Entry[100];
	private File dataBase;
	private Scanner fileReader;
	
	/**************************************************
	Database constructor
	Input to the method: address of the file to be read in
	Output(Return value): none
	Brief description of the task: creates a new database
	 	and populates it from a file
	Author: John Lawniczak
	**************************************************/
	public Database(String addr) throws Exception{
		fileAddr = addr;
		dataBase = new File(fileAddr);
		fileReader = new Scanner(dataBase);
		numberOfEntries = 0;
		processDatabase();
	}
	/**************************************************
	Method Name: addEntry
	Input to the method: String entry info
	Output(Return value): none
	Brief description of the task: adds an entry to the database
	Author: John Lawniczak
	**************************************************/
	public void addEntry(String entryInfo){
		list[numberOfEntries] = new Entry(entryInfo);
		numberOfEntries++;
	}
	
	/**************************************************
	Method Name: processDatabase
	Input to the method: none
	Output(Return value):none
	Brief description of the task: this private method 
		processes the file made in the constructor and
		adds valid lines from the file to the database
	Author: John Lawniczak
	 * @throws Exception 
	**************************************************/
	private void processDatabase() throws Exception{
		
		for(int i=0; i<100;i++){
			try{
				if(fileReader.hasNext()){
					String line = fileReader.nextLine();
					if(line.split(":").length==3){
						String name = line.split(":")[0];
						String addr = line.split(":")[1];
						String phone = line.split(":")[2];
						
						//check the formatting of the parts of the file line
						boolean nameCorrect = FormatChecker.nameMatchesFormatting(name);
						boolean addrCorrect = FormatChecker.addressMatchesFormatting(addr);
						boolean phoneCorrect = FormatChecker.phoneNumberMatchesFormatting(phone);

						if(nameCorrect && addrCorrect && phoneCorrect){
							this.addEntry(line);
						}
					}
				}
			}catch(Exception e){
				list[i]=null;
				throw e;
			}
		}
	}

	/**************************************************
	Method Name:getEntry
	Input to the method: int index of entry
	Output(Return value): Entry
	Brief description of the task: gets the entry at the given index
	Author: John Lawniczak
	**************************************************/
	public Entry getEntry(int i){
		return list[i];
	}

	/**************************************************
	Method Name:getNumEntries
	Input to the method: none
	Output(Return value): int number of entries
	Brief description of the task: gets the current number of entries
	Author: John Lawniczak
	**************************************************/
	public int getNumEntries(){
		return list.length+1;
	}
	
	/**************************************************
	Method Name:findIndicesOfName
	Input to the method: String name
	Output(Return value): int array of indices of the names
	Brief description of the task: returns an array with 
		the indices of the name given the rest of the
		array has -1 in it to signify that there are no 
		more matches found
	Author: John Lawniczak
	**************************************************/ 
	public int[] findIndicesOfName(String name){
		int[] indicesOfName = new int [numberOfEntries];
		int numberOfNamesFound =0;
		
		//loop through the list of entries looking for the matching name
		int i;
		for(i=0; i<numberOfEntries;i++){
			String temp = list[i].getName();
			if(temp.equals(name)){//we found a matching name so store its index to our return array
				indicesOfName[i] = (i);
				numberOfNamesFound++;
			}else{
				indicesOfName[i]=-1;
			}
		}
		return indicesOfName;
	}

	/**************************************************
	Method Name:findIndicesOfPhoneNumber
	Input to the method: String Phone number
	Output(Return value): int array of indices of the 
		phone numbers
	Brief description of the task: returns an array with 
		the indices of the phone number given the rest 
		of the array has -1 in it to signify that there
		are no more matches found
	Author: John Lawniczak
	**************************************************/ 
	public int[] findIndicesOfPhoneNumber(String phoneNum){
		int[] indicesOfPhoneNumbers = new int [numberOfEntries];
		int numberOfPhoneNumbersFound =0;
		
		//loop through the list of entries looking for the matching phone number  
		for(int i=0; i<numberOfEntries;i++){
			if(list[i].getPhoneNumber().equals(phoneNum)){	//we found a matching phone number so store it's index and move to look for the next one
				indicesOfPhoneNumbers[i] = i;
				numberOfPhoneNumbersFound++;
			}else{
				indicesOfPhoneNumbers[i]=-1;
			}
		}
		return indicesOfPhoneNumbers;
	}
	
	
	/**************************************************
	Method Name:findIndicesOfAddress
	Input to the method: String Address
	Output(Return value): int array of indices of the 
		Address
	Brief description of the task: returns an array with 
		the indices of the Address given the rest 
		of the array has -1 in it to signify that there
		are no more matches found
	Author: John Lawniczak
	**************************************************/ 
	public int[] findIndicesOfAddress(String address){
		int[] indicesOfAddresses = new int [numberOfEntries];
		int numberOfAddressesFound =0;
		
		//loop through the list of entries looking for the matching Address
		for(int i=0; i<numberOfEntries;i++){
			if(list[i].getAddress().contains(address)){	//we found a matching Address so store it's index and move to look for the next one
				indicesOfAddresses[i] = i;
				numberOfAddressesFound++;
			}else{
				indicesOfAddresses[i]=-1;
			}
		}
		return indicesOfAddresses;
	}
	
	/**************************************************
	Method Name: toString
	Input to the method: none
	Output(Return value): String
	Brief description of the task: compiles a string of 
		all the entries in the database
	Author: John LAwniczak
	**************************************************/
	public String toString()
	{
		String returnString="";
		for(int i=0; i<numberOfEntries;i++){
			returnString = returnString.concat(list[i] + "\n");
		}
		return returnString;
	}
	
}
